package com.hqyj.springBoot.modules.test.controller;


import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.test.entity.Student;
import com.hqyj.springBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student ----post
     * {"name":"zjz1","age":18}
     * {"id":13,"name":"zhang123","age":100}
     */
    @PostMapping(value = "/student",consumes = MediaType.APPLICATION_JSON_VALUE)
    Result<Student> insertStudent(@RequestBody Student student){
        return studentService.insertStudent(student);
    }


    /**
     * 127.0.0.1/api/student ----put
     * {"name":"zjz1","age":18}
     */
    @PutMapping(value = "/student",consumes = MediaType.APPLICATION_JSON_VALUE)
    Result<Student> updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }


    /**
     * 127.0.0.1/api/student/13 ----delete
     */
    @DeleteMapping("/student/{id}")
    Result<Object> deleteStudentById(@PathVariable int id){
        return studentService.deleteStudentById(id);
    }

    /**
     * 127.0.0.1/api/student/1 ----get
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1/api/students -----post
     * 在jpa中,sort字段传递的不再是列名,而是属性名
     * {"currentPage":1,"pageSize":5,"sort":"city_id","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/students",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Page<Student> getStudentBySearch(@RequestBody Search search) {
        return studentService.getStudentBySearch(search);
    }


    /**
     * 127.0.0.1/api/student?name=zjz1 ----get
     */
    @GetMapping("/student")
    Student findFirstByName(@RequestParam String name){
        return studentService.findFirstByName(name);
    }
}
